<template>
  <view class="permission-list-container">
    <view class="header-section">
      <text class="page-title">权限管理</text>
    </view>
    
    <view class="toolbar-section">
      <button class="action-button" @click="showCreatePermissionModal">创建权限</button>
    </view>
    
    <view class="permission-list">
      <view class="permission-item" v-for="permission in permissionList" :key="permission.id">
        <view class="permission-info">
          <text class="permission-name">{{ permission.name }}</text>
          <text class="permission-description">{{ permission.description }}</text>
        </view>
        <view class="permission-actions">
          <button class="action-button small" @click="editPermission(permission)">编辑</button>
          <button class="action-button small danger" @click="deletePermission(permission)">删除</button>
        </view>
      </view>
    </view>
    
    <view class="empty-state" v-if="permissionList.length === 0">
      <text class="empty-text">暂无权限数据</text>
    </view>
    
    <!-- 创建/编辑权限弹窗 -->
    <view class="modal" v-if="showPermissionModal">
      <view class="modal-content">
        <view class="modal-header">
          <text class="modal-title">{{ isEditPermission ? '编辑权限' : '创建权限' }}</text>
          <button class="close-button" @click="closePermissionModal">×</button>
        </view>
        <view class="modal-body">
          <view class="form-item">
            <text class="form-label">权限名称</text>
            <input class="form-input" v-model="permissionForm.name" placeholder="请输入权限名称" />
          </view>
          <view class="form-item">
            <text class="form-label">权限描述</text>
            <textarea class="form-textarea" v-model="permissionForm.description" placeholder="请输入权限描述"></textarea>
          </view>
        </view>
        <view class="modal-footer">
          <button class="action-button cancel" @click="closePermissionModal">取消</button>
          <button class="action-button" @click="savePermission">保存</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { permissionApi } from '../../../src/services/system'

// 定义权限类型
interface Permission {
  id: number
  name: string
  description: string
}

// 权限列表
const permissionList = ref<Permission[]>([])

// 是否显示权限弹窗
const showPermissionModal = ref(false)

// 是否为编辑权限模式
const isEditPermission = ref(false)

// 当前编辑的权限
const currentPermission = ref<Permission>({ id: 0, name: '', description: '' })

// 权限表单
const permissionForm = ref({
  name: '',
  description: ''
})

// 页面加载时获取权限列表
onMounted(() => {
  fetchPermissionList()
})

// 获取权限列表
const fetchPermissionList = async () => {
  try {
    const response: any = await permissionApi.getPermissionList()
    permissionList.value = response.data
    
    uni.showToast({
      title: '获取权限列表成功',
      icon: 'success'
    })
  } catch (error) {
    console.error('获取权限列表失败:', error)
    uni.showToast({
      title: '获取权限列表失败',
      icon: 'none'
    })
  }
}

// 显示创建权限弹窗
const showCreatePermissionModal = () => {
  isEditPermission.value = false
  permissionForm.value = { name: '', description: '' }
  showPermissionModal.value = true
}

// 编辑权限
const editPermission = (permission: Permission) => {
  isEditPermission.value = true
  currentPermission.value = { ...permission }
  permissionForm.value = { name: permission.name, description: permission.description }
  showPermissionModal.value = true
}

// 删除权限
const deletePermission = async (permission: Permission) => {
  uni.showModal({
    title: '确认删除',
    content: `确定要删除权限 "${permission.name}" 吗？`,
    success: async (res) => {
      if (res.confirm) {
        try {
          await permissionApi.deletePermission(permission.id)
          
          uni.showToast({
            title: '删除权限成功',
            icon: 'success'
          })
          
          // 重新获取权限列表
          fetchPermissionList()
        } catch (error) {
          console.error('删除权限失败:', error)
          uni.showToast({
            title: '删除权限失败',
            icon: 'none'
          })
        }
      }
    }
  })
}

// 关闭权限弹窗
const closePermissionModal = () => {
  showPermissionModal.value = false
}

// 保存权限
const savePermission = async () => {
  // 表单验证
  if (!permissionForm.value.name) {
    uni.showToast({
      title: '请输入权限名称',
      icon: 'none'
    })
    return
  }
  
  try {
    if (isEditPermission.value && currentPermission.value.id) {
      // 编辑权限
      await permissionApi.updatePermission(currentPermission.value.id, {
        name: permissionForm.value.name,
        description: permissionForm.value.description
      })
      
      uni.showToast({
        title: '更新权限成功',
        icon: 'success'
      })
    } else {
      // 创建权限
      await permissionApi.createPermission({
        name: permissionForm.value.name,
        description: permissionForm.value.description
      })
      
      uni.showToast({
        title: '创建权限成功',
        icon: 'success'
      })
    }
    
    // 关闭弹窗
    closePermissionModal()
    
    // 重新获取权限列表
    fetchPermissionList()
  } catch (error) {
    console.error('保存权限失败:', error)
    uni.showToast({
      title: '保存权限失败',
      icon: 'none'
    })
  }
}
</script>

<style lang="scss" scoped>
.permission-list-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20rpx;
}

.header-section {
  background-color: #fff;
  padding: 30rpx;
  margin-bottom: 20rpx;
  border-radius: 10rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  
  .page-title {
    font-size: 36rpx;
    font-weight: bold;
    color: #333;
    text-align: center;
  }
}

.toolbar-section {
  background-color: #fff;
  padding: 20rpx;
  margin-bottom: 20rpx;
  border-radius: 10rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  text-align: right;
}

.action-button {
  background-color: #4B0082;
  color: #fff;
  border: none;
  padding: 15rpx 30rpx;
  border-radius: 10rpx;
  font-size: 28rpx;
  
  &.small {
    padding: 10rpx 20rpx;
    font-size: 24rpx;
  }
  
  &.danger {
    background-color: #f44336;
  }
  
  &.cancel {
    background-color: #999;
  }
}

.permission-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.permission-item {
  background-color: #fff;
  border-radius: 10rpx;
  padding: 30rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.permission-info {
  display: flex;
  flex-direction: column;
  gap: 10rpx;
}

.permission-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.permission-description {
  font-size: 28rpx;
  color: #666;
}

.permission-actions {
  display: flex;
  gap: 10rpx;
}

.empty-state {
  text-align: center;
  padding: 100rpx 0;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
}

.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background-color: #fff;
  border-radius: 10rpx;
  width: 90%;
  max-width: 600rpx;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 2rpx solid #f5f5f5;
}

.modal-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.close-button {
  background: none;
  border: none;
  font-size: 40rpx;
  color: #999;
  padding: 0;
  width: 50rpx;
  height: 50rpx;
  display: flex;
  justify-content: center;
  align-items: center;
}

.modal-body {
  padding: 30rpx;
}

.form-item {
  margin-bottom: 30rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
}

.form-label {
  display: block;
  font-size: 28rpx;
  color: #333;
  margin-bottom: 10rpx;
}

.form-input {
  background-color: #f5f5f5;
  padding: 20rpx;
  border-radius: 10rpx;
  font-size: 28rpx;
  width: 100%;
  box-sizing: border-box;
}

.form-textarea {
  background-color: #f5f5f5;
  padding: 20rpx;
  border-radius: 10rpx;
  font-size: 28rpx;
  width: 100%;
  height: 150rpx;
  box-sizing: border-box;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 20rpx;
  padding: 30rpx;
  border-top: 2rpx solid #f5f5f5;
}
</style>